package com.hotacorp.opencloud.userservice.service.impl;

import java.util.Arrays;
import java.util.Date;
import java.util.List;
import java.util.Map;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.hotacorp.opencloud.common.mybatisplus.PageData;
import com.hotacorp.opencloud.common.mybatisplus.Query;
import com.hotacorp.opencloud.userservice.dao.SysRoleDao;
import com.hotacorp.opencloud.userservice.entity.SysRoleEntity;
import com.hotacorp.opencloud.userservice.service.SysRoleMenuService;
import com.hotacorp.opencloud.userservice.service.SysRoleService;
import com.hotacorp.opencloud.userservice.service.SysUserRoleService;

/**
 * 角色管理接口实现
 * @author lwg
 *
 */
@Service("sysRoleService")
public class SysRoleServiceImpl extends ServiceImpl<SysRoleDao, SysRoleEntity> implements SysRoleService {
	@Autowired
	private SysRoleMenuService sysRoleMenuService;
	@Autowired
	private SysUserRoleService sysUserRoleService;

    @Override
    public PageData queryPage(Map<String, Object> params) {
    	QueryWrapper<SysRoleEntity> query = new QueryWrapper<SysRoleEntity>();
    	query.eq("tenantid", params.get("tenantid"));
    	if (StringUtils.isNotBlank((String)params.get("roleName"))) query.like("role_name", "%"+ params.get("roleName") + "%");
        IPage<SysRoleEntity> page = this.page(
                new Query<SysRoleEntity>().getPage(params),
                query
        );

        return new PageData(page);
    }

	@Override
	public List<SysRoleEntity> select(Map<String, Object> params) {
		QueryWrapper<SysRoleEntity> wrap = new QueryWrapper<SysRoleEntity>();
		if (params.containsKey("tenantid")) {
			wrap.eq("tenantid", params.get("tenantid"));
		}
		return super.list(wrap);
	}

	@Override
    @Transactional(rollbackFor = Exception.class)
	public void saveRole(SysRoleEntity role, Long tenantid) {
		role.setTenantid(tenantid);
        role.setCreateTime(new Date());
        this.save(role);

        //保存角色与菜单关系
        sysRoleMenuService.saveOrUpdate(role.getRoleId(), role.getMenuIdList(), tenantid);
	}

	@Override
    @Transactional(rollbackFor = Exception.class)
	public void update(SysRoleEntity role, Long tenantid) {
		SysRoleEntity sdfs = this.getOne((new QueryWrapper<SysRoleEntity>()).eq("role_id", role.getRoleId()).eq("tenantid", tenantid));
		if (sdfs == null) {
			throw new RuntimeException("非法操作数据");
		}
        this.updateById(role);

        //更新角色与菜单关系
        sysRoleMenuService.saveOrUpdate(role.getRoleId(), role.getMenuIdList(), tenantid);
	}

	@Override
    @Transactional(rollbackFor = Exception.class)
	public void deleteBatch(Long[] roleIds, Long tenantid) {
        //删除角色
        this.remove((new QueryWrapper<SysRoleEntity>()).in("role_id", Arrays.asList(roleIds)).eq("tenantid", tenantid));

        //删除角色与菜单关联
        sysRoleMenuService.removeRoles(Arrays.asList(roleIds), tenantid);

        //删除角色与用户关联
        sysUserRoleService.removeRoles(Arrays.asList(roleIds), tenantid);
	}

}